package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.common.core.domain.model.LoginUser;
import com.loan.common.helper.LoginHelper;
import com.loan.system.domain.OverWeightEntity;
import com.loan.system.mapper.OverWeightMapper;
import com.loan.system.mapper.UserOverMapper;
import com.loan.system.service.OverWeightService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author shiguang on 2022/9/30 9:07
 */
@Service
@RequiredArgsConstructor
public class OverWeightServiceImpl implements OverWeightService {

    private final OverWeightMapper overWeightMapper;

    private final UserOverMapper userOverMapper;


    @Override
    public List<OverWeightEntity> list(Long overId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        //根据催收配置  获取该配置下绑定的用户
        List<Long> userIds = userOverMapper.selectUserIds(overId);
        if (CollectionUtils.isEmpty(userIds)){
            return null;
        }
        List<OverWeightEntity> list = overWeightMapper.selectOverList(userIds,overId);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveOrUpdateOverWeight(List<OverWeightEntity> list) {
        boolean flag = overWeightMapper.insertOrUpdateBatch(list);
        if (flag){
            return 1;
        }
        return 0;
    }

    @Override
    public void deleteUserIdNotInId(Long id, List userIds) {
        overWeightMapper.delete(new QueryWrapper<OverWeightEntity>().eq("over_config_id",id).notIn("user_id",userIds));
    }

    @Override
    public List<OverWeightEntity> selectListByOverId(Long overConfigId) {
        return overWeightMapper.selectList(new QueryWrapper<OverWeightEntity>().eq("over_config_id",overConfigId).eq("status","2"));

    }
}
